package com.hzit.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.common.utils.PageUtils;
import com.hzit.common.utils.Query;
import com.hzit.shop.dao.TypeTemplateDao;
import com.hzit.shop.entity.SpecificationOptionEntity;
import com.hzit.shop.entity.TypeTemplateEntity;
import com.hzit.shop.service.SpecificationOptionService;
import com.hzit.shop.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("typeTemplateService")
public class TypeTemplateServiceImpl extends ServiceImpl<TypeTemplateDao, TypeTemplateEntity> implements TypeTemplateService {
	@Autowired
	private SpecificationOptionService optionService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TypeTemplateEntity> page = this.page(
                new Query<TypeTemplateEntity>().getPage(params),
                new QueryWrapper<TypeTemplateEntity>()
        );

        return new PageUtils(page);
    }

    //2. 根据模板id查询规格及规格选项列表
	@Override
	public List<Map> findSpecAndOptions(String id) {
    	//2.1 根据id查询模板对象
		TypeTemplateEntity templateEntity = this.getById(id);
		//2.2 得到规格列表
		String specIds = templateEntity.getSpecIds();
		//2.3 转换为List<Map>对象
		List<Map> maps = JSON.parseArray(specIds, Map.class);
		//2.4 遍历mpas为每个map指定一个options属性
		for (Map map : maps) {
			//2.4.1 得到规格id
			String specId = map.get("id").toString();
			//2.4.2 根据规格id查询出对应的规格选项列表
			List<SpecificationOptionEntity> options = optionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", specId));
			//2.4.3 将当前的options指定给map对象
			map.put("options",options);
		}
		return maps;
	}

}